package org.mushroomdb.security.queries;

import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.query.Query;
import org.mushroomdb.security.SecurityService;
import org.mushroomdb.security.User;
import org.mushroomdb.service.ServiceManager;

/**
 * @author Tomas
 *
 * Representation of the "Create User" Query 
 */
public class CreateUserQuery extends SecurityQuery {
    private String userName;

    private String password;

    
    public CreateUserQuery(String query) {
        query = query.substring(query.indexOf("USER") + 4).trim();
        this.userName = query.substring(0, query.indexOf(" ")).trim();

        query = query
                .substring(query.indexOf("PASSWORD") + "PASSWORD".length())
                .trim();
        this.password = query.substring(0, query.indexOf(";")).trim();

    }

    public Object execute(EvaluationEngine evaluationEngine) {

        
        SecurityService service = (SecurityService) ServiceManager.getInstance().getService(SecurityService.class);

        User user = service.createUser(this.userName, this.password);
        return user;

    }

}
